####################################################################################
###
###   Immigration, Public Housing and Support for the French National Front
###   Gloria Gennaro
###
###   Paper Tab 1, Table A1, test for pre-trends 
###   NB: Tab 1 and A1 report the same estimates. Tab A1 also shows control coefficients
###
####################################################################################


library(AER)
library(dplyr)
library(xtable)


################################################################################
# Set Up
################################################################################

# Load Working Directories
source(paste0(wd_main, '/2_code/00_working_directories.R'))

# Load data and clean
source(paste0(wd_code, '/01_data_load_clean.R'))


################################################################################
# Test of pre-trends joint significance 
################################################################################


modelli = list()
for (n in c(1,2,3)){
  print(paste0('Immigation tercile ', n, ' -------------- '))
  df1 = df_did[which(!is.na(df_did$fn) & df_did$imm_quant_99==n),]
  temp = lm(data=df1, fn ~  m4 + m3 + m2 + p1 + p2 + p3 + running + factor(CODGEO) + factor(policy_period) )
  f_reduced = lm(data=df1, fn ~ p1 + p2 + p3 + running + factor(CODGEO) + factor(policy_period) )
  anova(f_reduced,temp,test="F") %>% print()
}


################################################################################
# Main regression table
###############################################################################

### Columns 1-6

for (n in c(1,2,3)){
  print(paste0('--------------Immigation tercile ', n, ' -------------- '))
  df1 = df_did[which(!is.na(df_did$fn) & !is.na(df_did$imm_share_90) & !is.na(df_did$hlm_share_90) & df_did$imm_quant_99==n),]
  
  # Models without controls
  print(paste0(' * Without controls *'))
  temp = lm(data=df1, fn ~  postT + running + factor(CODGEO) + factor(policy_period))
  r2 = summary(temp)$r.squared
  temp = coeftest(temp, vcov = vcovCL, cluster = ~df1$CODGEO)
  
  # Print out results
    temp["postT", 1:2] %>% round(3) %>% paste0(collapse=", ") %>% paste0("Coeff and SE of Policy: ", .) %>% print()
    attr(temp, 'nobs') %>% round(3) %>% paste0("Observations: ", .) %>% print()
    mean(df1$fn) %>% round(3) %>% paste0("Mean DV: ", .) %>% print()
    round(r2, 3) %>% round(3) %>% paste0("R-squared: ", .) %>% print()
    
  # Models with controls
  print(paste0(' * With controls *'))
  temp = lm(data=df1, fn ~  postT + res_owned_share_90*policy_period + hlm_share_90*policy_period + imm_share_90*policy_period + running + factor(CODGEO) + factor(policy_period) )
  r2 = summary(temp)$r.squared
  temp = coeftest(temp, vcov = vcovCL, cluster = ~df1$CODGEO)
  
  # Print out results
    temp["postT", 1:2] %>% round(3) %>% paste0(collapse=", ") %>% paste0("Coeff and SE of Policy: ", .) %>% print()
    temp["res_owned_share_90:policy_period", 1:2] %>% round(3) %>% paste0(collapse=", ") %>% paste0("Coeff and SE of Time × Home Ownership 90: ", .) %>% print()
    temp["policy_period:hlm_share_90", 1:2] %>% round(3) %>% paste0(collapse=", ") %>% paste0("Coeff and SE of Time × Public Housing 90: ", .) %>% print()
    temp["policy_period:imm_share_90", 1:2] %>% round(3) %>% paste0(collapse=", ") %>% paste0("Coeff and SE of Time × Immigration 90: ", .) %>% print()
    attr(temp, 'nobs') %>% round(3) %>% paste0("Observations: ", .) %>% print()
    mean(df1$fn) %>% round(3) %>% paste0("Mean DV: ", .) %>% print()
    round(r2, 3) %>% round(3) %>% paste0("R-squared: ", .) %>% print()
}


print(paste0('-------------- Interacted models --------------'))

### Column 7

print(paste0(' * Without controls *'))

df1 = df_did[which(!is.na(df_did$fn) & !is.na(df_did$imm_share_90) & !is.na(df_did$hlm_share_90)),]
temp = lm(data=df1, fn ~  postT*factor(imm_quant_99) + running + factor(CODGEO) + factor(policy_period) )
r2 = summary(temp)$r.squared
temp = coeftest(temp, vcov = vcovCL, cluster = ~df1$CODGEO)

# Print out results
temp["postT", 1:2] %>% round(3) %>% paste0(collapse=", ") %>% paste0("Coeff and SE of  Policy: ", .) %>% print()
temp["postT:factor(imm_quant_99)2", 1:2] %>% round(3) %>% paste0(collapse=", ") %>% paste0("Coeff and SE of Policy×Medium: ", .) %>% print()
temp["postT:factor(imm_quant_99)3", 1:2] %>% round(3) %>%paste0(collapse=", ") %>% paste0("Coeff and SE of Policy×High: ", .) %>% print()
attr(temp, 'nobs') %>% paste0("Observations: ", .) %>% print()
mean(df1$fn) %>% round(3) %>% paste0("Mean DV: ", .) %>% print()
round(r2, 3) %>% paste0("R-squared: ", .) %>% print()

### Column 8

print(paste0(' * With controls *'))

df1 = df_did[which(!is.na(df_did$fn) & !is.na(df_did$imm_share_90) & !is.na(df_did$hlm_share_90)),]
temp = lm(data=df1, fn ~  postT*factor(imm_quant_99) + running +  res_owned_share_90*policy_period + hlm_share_90*policy_period + imm_share_90*policy_period + factor(CODGEO) + factor(policy_period) )
r2 = summary(temp)$r.squared
temp = coeftest(temp, vcov = vcovCL, cluster = ~df1$CODGEO)

temp["postT", 1:2]  %>% round(3) %>% paste0(collapse=", ") %>% paste0("Coeff and SE of Policy: ", .) %>% print()
temp["postT:factor(imm_quant_99)2", 1:2] %>% round(3) %>% paste0(collapse=", ") %>% paste0("Coeff and SE of Policy×Medium: ", .) %>% print()
temp["postT:factor(imm_quant_99)3", 1:2] %>% round(3) %>% paste0(collapse=", ") %>% paste0("Coeff and SE of Policy×High: ", .) %>% print()
temp["res_owned_share_90:policy_period", 1:2] %>% round(3) %>% paste0(collapse=", ") %>% paste0("Coeff and SE of Time × Home Ownership 90: ", .) %>% print()
temp["policy_period:hlm_share_90", 1:2] %>% round(3) %>% paste0(collapse=", ") %>% paste0("Coeff and SE of Time × Public Housing 90: ", .) %>% print()
temp["policy_period:imm_share_90", 1:2] %>% round(3) %>% paste0(collapse=", ") %>% paste0("Coeff and SE of Time × Immigration 90: ", .) %>% print()
attr(temp, 'nobs') %>% paste0("Observations: ", .) %>% print()
mean(df1$fn) %>% round(3) %>% paste0("Mean DV: ", .) %>% print()
round(r2, 3) %>% paste0("R-squared: ", .) %>% print()


